package ma.xin.shi;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import ma.xin.shi.configurations.ECU.ECUEntry;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.MessageBox;
import java.util.List;

public class EcuDisplay {
	private static Table ecuTable;
	private static Text ecuNameText, macAddrText, initDelayMinText, initDelayMaxText;
    private static Text repetitionBaseDelayText, repetionsMaxText, cyclicOfferDelayText;
    private static Text requestResponseDelayMinText, requestResponseDelayMaxText;
    private static ECUEntry editingEntry = null;     // 在类顶部添加这个变量

    public static void display(Composite workArea) {
        for (Control control : workArea.getChildren()) {
            control.dispose();
        }

        Composite[] composites = addECUComposite(workArea);
        CTabFolder tabFolder = (CTabFolder) composites[0];
        Composite dataListComposite = composites[1];
        Composite workAreaComposite = composites[2];

        GridData dataListData = new GridData(SWT.FILL, SWT.FILL, false, true);
        dataListData.widthHint = 200;
        dataListComposite.setLayoutData(dataListData);
        dataListComposite.setBackground(workArea.getDisplay().getSystemColor(SWT.COLOR_WHITE));

        // add Tabs
        addTabToFolder(tabFolder, "Someip", dataListComposite, workAreaComposite);
        addTabToFolder(tabFolder, "DDS", dataListComposite, workAreaComposite);
        addTabToFolder(tabFolder, "IPC", dataListComposite, workAreaComposite);

        // 设置默认加载第一个 Tab 的内容
        if (tabFolder.getItemCount() > 0) {
            String defaultTabName = tabFolder.getItem(0).getText();
            dataListAreaCtrl(dataListComposite, defaultTabName);
            ECUWorkAreaCtrl(workAreaComposite, defaultTabName);
        }

        // 监听 Tab 切换事件
        tabFolder.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                CTabItem selectedTab = tabFolder.getSelection();
                String selectedTabName = selectedTab.getText();

                dataListAreaCtrl(dataListComposite, selectedTabName);
                ECUWorkAreaCtrl(workAreaComposite, selectedTabName);
            }
        });

        workArea.layout(true, true);
    }

	private static Composite[] addECUComposite(Composite parent) {
	    Composite mainArea = new Composite(parent, SWT.NONE);
	    GridLayout layout = new GridLayout(1, false);
	    layout.verticalSpacing = 0;
	    mainArea.setLayout(layout);

	    GridData mainAreaData = new GridData(SWT.FILL, SWT.FILL, true, true);
	    mainArea.setLayoutData(mainAreaData);

	    CTabFolder tabFolder = new CTabFolder(mainArea, SWT.BORDER | SWT.FLAT);
	    tabFolder.setSimple(false);
	    tabFolder.setBorderVisible(false);
	    tabFolder.setTabHeight(24);

	    GridData tabFolderLayoutData = new GridData(SWT.FILL, SWT.TOP, true, false);
	    tabFolderLayoutData.heightHint = 28;
	    tabFolder.setLayoutData(tabFolderLayoutData);

	    Composite contentArea = new Composite(mainArea, SWT.NONE);
	    GridLayout contentLayout = new GridLayout(2, false);
	    contentLayout.verticalSpacing = 0;
	    contentArea.setLayout(contentLayout);

	    contentArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

	    Composite dataListComposite = new Composite(contentArea, SWT.BORDER);
	    dataListComposite.setLayout(new GridLayout(1, false));

	    GridData dataListData = new GridData(SWT.LEFT, SWT.FILL, false, true);
	    dataListData.verticalIndent = 0;
	    dataListComposite.setLayoutData(dataListData);

	    Composite workAreaComposite = new Composite(contentArea, SWT.BORDER);
	    workAreaComposite.setLayout(new GridLayout(1, false));

	    GridData workAreaData = new GridData(SWT.FILL, SWT.FILL, true, true);
	    workAreaData.verticalIndent = 0;
	    workAreaComposite.setLayoutData(workAreaData);

	    return new Composite[]{tabFolder, dataListComposite, workAreaComposite};
	}

	private static void addTabToFolder(CTabFolder tabFolder, String tabName, Composite dataListComposite, Composite workAreaComposite) {
        CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
        tabItem.setText(tabName);

        if (tabFolder.getData("listener-added") == null) {
            tabFolder.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    CTabItem selectedTab = tabFolder.getSelection();
                    String selectedTabName = selectedTab.getText();
                    //updateContentAreas(dataListComposite, workAreaComposite, selectedTabName);
                    dataListAreaCtrl(dataListComposite, selectedTabName);
                }
            });

            tabFolder.setData("listener-added", Boolean.TRUE);
        }

        // set default tab
        tabFolder.setSelection(tabFolder.getItem(0));//tabFolder.setSelection(tabFolder.getItemCount() - 1);
    }
	
	private static void dataListAreaCtrl(Composite parent, String selectedTabName)
	{
		for (Control child : parent.getChildren()) {
	        child.dispose();
	    }

		if ("Someip".equals(selectedTabName)) {
			// add data List button [Add, Del]
		    addECUDatalistButton(parent);
		    // add data list table
		    addECUDatalistTable(parent);
	    } else if ("DDS".equals(selectedTabName)) {
		    addECUDatalistButton(parent);
		    addECUDatalistTable(parent);
	    } else if ("IPC".equals(selectedTabName)) {
		    addECUDatalistButton(parent);
		    addECUDatalistTable(parent);
	    } else {
		    addECUDatalistButton(parent);
		    addECUDatalistTable(parent);
	    }

		parent.layout();
	}
	
	private static void ECUWorkAreaCtrl(Composite parent, String selectedTabName)
	{
		for (Control child : parent.getChildren()) {
	        child.dispose();
	    }

		if ("Someip".equals(selectedTabName)) {
			addECUSomeipWorkAreaTable(parent);
	    } else if ("DDS".equals(selectedTabName)) {
	    	addECUDDSWorkAreaTable(parent);
	    } else if ("IPC".equals(selectedTabName)) {
	    	addECUIPCWorkAreaTable(parent);
	    } else {
	    	;
	    }

		parent.layout(true, true);
	}
	
	private static void addECUDatalistButton(Composite parent) {
	    Composite buttonComposite = new Composite(parent, SWT.NONE);
	    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
	    rowLayout.spacing = 5;
	    buttonComposite.setLayout(rowLayout);

	    GridData buttonData = new GridData(SWT.FILL, SWT.TOP, true, false);
	    buttonComposite.setLayoutData(buttonData);

	    // Add 按钮
	    Button btnAdd = new Button(buttonComposite, SWT.PUSH);
	    btnAdd.setText("Add");

	    // Del 按钮
	    Button btnRemove = new Button(buttonComposite, SWT.PUSH);
	    btnRemove.setText("Del");

	    // Add Button listen event
	    btnAdd.addListener(SWT.Selection, event -> {
	        Shell shell = parent.getShell();
	        String[] result = showSomeipInputDialog(shell); // 返回 [ecuName, macAddr, ...]

	        if (result != null) {
	            ECUEntry entry = new ECUEntry();
	            entry.setEcuName(result[0]);
	            entry.setMacAddr(result[1]);
	            entry.setInitDelayMin(result[2]);
	            entry.setInitDelayMax(result[3]);
	            entry.setRepetitionBaseDelay(result[4]);
	            entry.setRepetitionsMax(result[5]);
	            entry.setCyclicOfferDelay(result[6]);
	            entry.setRequestResponseDelayMin(result[7]);
	            entry.setRequestResponseDelayMax(result[8]);

	            // 保存到配置系统
	            ConfigurationManager.getInstance().getECUConfig().addEcu(entry);

	            // 更新表格
	            TableItem item = new TableItem(ecuTable, SWT.NONE);
	            item.setText(0, String.valueOf(ecuTable.getItemCount()));
	            item.setText(1, entry.getEcuName());
	            item.setData(entry); // 存储完整数据对象
	        }
	    });

	    // Del Button listen event
	    btnRemove.addListener(SWT.Selection, event -> {
	        int selectedIndex = ecuTable.getSelectionIndex();

	        if (selectedIndex == -1) {
	            MessageBox messageBox = new MessageBox(parent.getShell(), SWT.ICON_WARNING | SWT.OK);
	            messageBox.setMessage("请选择要删除的行。");
	            messageBox.setText("提示");
	            messageBox.open();
	        } else {
	            TableItem[] selectedItems = ecuTable.getSelection();
	            for (TableItem item : selectedItems) {
	                ECUEntry entry = (ECUEntry) item.getData();
	                if (entry != null) {
	                    // 使用 removeECU(String name)
	                    ConfigurationManager.getInstance().getECUConfig().removeECU(entry.getEcuName());
	                }
	                item.dispose(); // 从表格中移除
	            }

	            updateTableIndices(); // 刷新序号
	            clearInputFields(); // 可选：清空输入框
	        }
	    });

	}

	private static void clearInputFields() {
	    ecuNameText.setText("");
	    macAddrText.setText("");
	    initDelayMinText.setText("");
	    initDelayMaxText.setText("");
	    repetitionBaseDelayText.setText("");
	    repetionsMaxText.setText("");
	    cyclicOfferDelayText.setText("");
	    requestResponseDelayMinText.setText("");
	    requestResponseDelayMaxText.setText("");
	}

	private static void updateTableIndices() {
	    TableItem[] items = ecuTable.getItems();
	    for (int i = 0; i < items.length; i++) {
	        items[i].setText(0, String.valueOf(i + 1)); // 更新序号，从 1 开始
	    }
	}

	private static String[] showSomeipInputDialog(Shell parent) {
	    Shell dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
	    dialogShell.setText("Add New ECU");
	    dialogShell.setLayout(new GridLayout(2, false));

	    // 创建输入字段
	    Text ecuNameText = createLabeledText(dialogShell, "ECU Name:");
	    Text macAddrText = createLabeledText(dialogShell, "MAC Address:");
	    Text initDelayMinText = createLabeledText(dialogShell, "Init_Delay_Min(ms):");
	    Text initDelayMaxText = createLabeledText(dialogShell, "Init_Delay_Max(ms):");
	    Text repetitionBaseDelayText = createLabeledText(dialogShell, "Repetition_Base_Delay(ms):");
	    Text repetionsMaxText = createLabeledText(dialogShell, "Repetions_Max:");
	    Text cyclicOfferDelayText = createLabeledText(dialogShell, "Cyclic_Offer_Delay(ms):");
	    Text requestResponseDelayMinText = createLabeledText(dialogShell, "Request_Response_Delay_Min(ms):");
	    Text requestResponseDelayMaxText = createLabeledText(dialogShell, "Request_Response_Delay_Max(ms):");

	    // 按钮区域
	    Composite buttonComp = new Composite(dialogShell, SWT.NONE);
	    buttonComp.setLayout(new GridLayout(2, false));
	    buttonComp.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));

	    Button okBtn = new Button(buttonComp, SWT.PUSH);
	    okBtn.setText("OK");
	    Button cancelBtn = new Button(buttonComp, SWT.PUSH);
	    cancelBtn.setText("Cancel");

	    final String[] result = new String[9]; // 存储结果
	    okBtn.addListener(SWT.Selection, e -> {
	        result[0] = ecuNameText.getText();
	        result[1] = macAddrText.getText();
	        result[2] = initDelayMinText.getText();
	        result[3] = initDelayMaxText.getText();
	        result[4] = repetitionBaseDelayText.getText();
	        result[5] = repetionsMaxText.getText();
	        result[6] = cyclicOfferDelayText.getText();
	        result[7] = requestResponseDelayMinText.getText();
	        result[8] = requestResponseDelayMaxText.getText();
	        dialogShell.close();
	    });

	    cancelBtn.addListener(SWT.Selection, e -> {
	        result[0] = null; // 标识取消操作
	        dialogShell.close();
	    });

	    dialogShell.pack();
	    dialogShell.open();

	    Display display = parent.getDisplay();
	    while (!dialogShell.isDisposed()) {
	        if (!display.readAndDispatch()) {
	            display.sleep();
	        }
	    }

	    return result[0] == null ? null : result;
	}

	/* ECU Someip 页面加载的数据列表区域内容 */
	private static void addECUDatalistTable(Composite parent) {
		// 创建表格控件
	    ecuTable = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
	    ecuTable.setHeaderVisible(true);  // 显示表头
	    ecuTable.setLinesVisible(true);   // 显示表格线

	    // 设置布局数据：占满父容器剩余空间
	    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
	    ecuTable.setLayoutData(gridData);

	    // 添加两列：Index 和 ECU Name
	    TableColumn indexColumn = new TableColumn(ecuTable, SWT.NONE);
	    indexColumn.setText("Index");
	    indexColumn.setWidth(50);  // 设置宽度

	    TableColumn nameColumn = new TableColumn(ecuTable, SWT.NONE);
	    nameColumn.setText("ECUName");
	    nameColumn.setWidth(150);  // 设置宽度

	    loadExistingEcusToTable(); //加载当前已经配置的所有数据
	}

	private static void loadExistingEcusToTable() {
	    // 1. 清空表格中的所有内容
	    if (ecuTable != null && ecuTable.getItems() != null) {
	        for (TableItem item : ecuTable.getItems()) {
	            item.dispose(); // 移除每个表项
	        }
	    }

	    // 2. 加载最新配置并更新表格
	    List<ECUEntry> allEcus = ConfigurationManager.getInstance().getECUConfig().getAllEcus();

	    for (int i = 0; i < allEcus.size(); i++) {
	        ECUEntry entry = allEcus.get(i);
	        TableItem item = new TableItem(ecuTable, SWT.NONE);
	        item.setText(0, String.valueOf(i + 1)); // 序号列，从1开始计数
	        item.setText(1, entry.getEcuName()); // 名称列
	        item.setData(entry); // 存储完整数据对象
	    }
	}

	public static void addECUSomeipWorkAreaTable(Composite parent) {
        // 设置主容器为 GridLayout，2列，自动扩展行数
        parent.setLayout(new GridLayout(2, false));

        // 创建所有 Label + Text 表单项
        ecuNameText = createLabeledText(parent, "ECU Name:");
        macAddrText = createLabeledText(parent, "MAC Address:");
        initDelayMinText = createLabeledText(parent, "Init_Delay_Min(ms):");
        initDelayMaxText = createLabeledText(parent, "Init_Delay_Max(ms):");
        repetitionBaseDelayText = createLabeledText(parent, "Repetition_Base_Delay(ms):");
        repetionsMaxText = createLabeledText(parent, "Repetions_Max:");
        cyclicOfferDelayText = createLabeledText(parent, "Cyclic_Offer_Delay(ms):");
        requestResponseDelayMinText = createLabeledText(parent, "Request_Response_Delay_Min(ms):");
        requestResponseDelayMaxText = createLabeledText(parent, "Request_Response_Delay_Max(ms):");

        // 添加空白占位符以撑开空间，使按钮靠底部显示
        Label placeholder = new Label(parent, SWT.NONE);
        placeholder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // 创建按钮区域：Span 两列，右下对齐
        Composite buttonComposite = new Composite(parent, SWT.NONE);
        buttonComposite.setLayout(new GridLayout(2, false));
        buttonComposite.setLayoutData(new GridData(SWT.END, SWT.BOTTOM, false, false));

        Button btnSave = new Button(buttonComposite, SWT.PUSH);
        btnSave.setText("Save");

        Button btnCancel = new Button(buttonComposite, SWT.PUSH);
        btnCancel.setText("Cancel");
        
        // 添加表格选择监听器
        ecuTable.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                TableItem selectedItem = (TableItem) e.item;
                if (selectedItem != null && selectedItem.getData() instanceof ECUEntry) {
                	editingEntry = (ECUEntry) selectedItem.getData();

                    // 填充输入框
                    //ecuNameText.setText(editingEntry.getEcuName());
                    //macAddrText.setText(editingEntry.getMacAddress());
                    //initDelayMinText.setText(editingEntry.getInitDelayMin());
                    //initDelayMaxText.setText(editingEntry.getInitDelayMax());
                    //repetitionBaseDelayText.setText(editingEntry.getRepetitionBaseDelay());
                    //repetionsMaxText.setText(editingEntry.getRepetitionsMax());
                    //cyclicOfferDelayText.setText(editingEntry.getCyclicOfferDelay());
                    //requestResponseDelayMinText.setText(editingEntry.getRequestResponseDelayMin());
                    //requestResponseDelayMaxText.setText(editingEntry.getRequestResponseDelayMax());
                    AutorsarCommonUtils.setSafeText(ecuNameText, editingEntry.getEcuName());
                    AutorsarCommonUtils.setSafeText(macAddrText, editingEntry.getMacAddress());
                    AutorsarCommonUtils.setSafeText(initDelayMinText, editingEntry.getInitDelayMin());
                    AutorsarCommonUtils.setSafeText(initDelayMaxText, editingEntry.getInitDelayMax());
                    AutorsarCommonUtils.setSafeText(repetitionBaseDelayText, editingEntry.getRepetitionBaseDelay());
                    AutorsarCommonUtils.setSafeText(repetionsMaxText, editingEntry.getRepetitionsMax());
                    AutorsarCommonUtils.setSafeText(cyclicOfferDelayText, editingEntry.getCyclicOfferDelay());
                    AutorsarCommonUtils.setSafeText(requestResponseDelayMinText, editingEntry.getRequestResponseDelayMin());
                    AutorsarCommonUtils.setSafeText(requestResponseDelayMaxText, editingEntry.getRequestResponseDelayMax());
                }
            }
        });
        
        // btnSave 事件
        btnSave.addListener(SWT.Selection, event -> {
            if (editingEntry == null) {
                MessageBox messageBox = new MessageBox(parent.getShell(), SWT.ICON_WARNING | SWT.OK);
                messageBox.setMessage("没有可保存的 ECU 条目。");
                messageBox.setText("警告");
                messageBox.open();
                return;
            }

            // 提示是否确认保存
            MessageBox confirmBox = new MessageBox(parent.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
            confirmBox.setMessage("确定要保存对 [" + editingEntry.getEcuName() + "] 的修改吗？");
            confirmBox.setText("确认保存");
            int response = confirmBox.open();

            if (response == SWT.YES) {
                // 更新 entry 数据
                editingEntry.setEcuName(ecuNameText.getText());
                editingEntry.setMacAddr(macAddrText.getText());
                editingEntry.setInitDelayMin(initDelayMinText.getText());
                editingEntry.setInitDelayMax(initDelayMaxText.getText());
                editingEntry.setRepetitionBaseDelay(repetitionBaseDelayText.getText());
                editingEntry.setRepetitionsMax(repetionsMaxText.getText());
                editingEntry.setCyclicOfferDelay(cyclicOfferDelayText.getText());
                editingEntry.setRequestResponseDelayMin(requestResponseDelayMinText.getText());
                editingEntry.setRequestResponseDelayMax(requestResponseDelayMaxText.getText());

                // 同步更新配置系统（如果需要）
                // 如果你有专门的 update 方法也可以调用
                ConfigurationManager.getInstance().getECUConfig().updateECU(editingEntry.getEcuName(), editingEntry);

                // 刷新表格中该行的显示
                refreshTableRow(editingEntry);

                // 可选：提示保存成功
                MessageBox infoBox = new MessageBox(parent.getShell(), SWT.ICON_INFORMATION | SWT.OK);
                infoBox.setMessage("ECU 修改已保存！");
                infoBox.setText("提示");
                infoBox.open();
            }
        });

        // btnCancel 事件
        btnCancel.addListener(SWT.Selection, event -> {
            if (editingEntry != null) {
                // 弹出确认对话框
                MessageBox confirmBox = new MessageBox(parent.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
                confirmBox.setMessage("放弃当前所有修改？");
                confirmBox.setText("确认取消");
                int response = confirmBox.open();

                if (response == SWT.YES) {
                    // 恢复原始数据
                    ecuNameText.setText(editingEntry.getEcuName());
                    macAddrText.setText(editingEntry.getMacAddress());
                    initDelayMinText.setText(editingEntry.getInitDelayMin());
                    initDelayMaxText.setText(editingEntry.getInitDelayMax());
                    repetitionBaseDelayText.setText(editingEntry.getRepetitionBaseDelay());
                    repetionsMaxText.setText(editingEntry.getRepetitionsMax());
                    cyclicOfferDelayText.setText(editingEntry.getCyclicOfferDelay());
                    requestResponseDelayMinText.setText(editingEntry.getRequestResponseDelayMin());
                    requestResponseDelayMaxText.setText(editingEntry.getRequestResponseDelayMax());
                }
            }
        });


    }

	private static void refreshTableRow(ECUEntry updatedEntry) {
	    TableItem[] items = ecuTable.getItems();
	    for (TableItem item : items) {
	        ECUEntry entry = (ECUEntry) item.getData();
	        if (entry != null && entry.getEcuName().equals(updatedEntry.getEcuName())) {
	            item.setText(1, updatedEntry.getEcuName()); // 只更新名称列作为示例
	            item.setData(updatedEntry); // 更新数据对象
	            break;
	        }
	    }
	}

	
	public static void addECUDDSWorkAreaTable(Composite parent) {
        // 设置主容器为 GridLayout，2列，自动扩展行数
        parent.setLayout(new GridLayout(2, false));

        // 创建所有 Label + Text 表单项
        ecuNameText = createLabeledText(parent, "ECU Name:");


        // 添加空白占位符以撑开空间，使按钮靠底部显示
        Label placeholder = new Label(parent, SWT.NONE);
        placeholder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // 创建按钮区域：Span 两列，右下对齐
        Composite buttonComposite = new Composite(parent, SWT.NONE);
        buttonComposite.setLayout(new GridLayout(2, false));
        buttonComposite.setLayoutData(new GridData(SWT.END, SWT.BOTTOM, false, false));

        Button btnSave = new Button(buttonComposite, SWT.PUSH);
        btnSave.setText("Save");

        Button btnCancel = new Button(buttonComposite, SWT.PUSH);
        btnCancel.setText("Cancel");
    }
	
	public static void addECUIPCWorkAreaTable(Composite parent) {
        // 设置主容器为 GridLayout，2列，自动扩展行数
        parent.setLayout(new GridLayout(2, false));

        // 创建所有 Label + Text 表单项
        ecuNameText = createLabeledText(parent, "ECU_Name:");


        // 添加空白占位符以撑开空间，使按钮靠底部显示
        Label placeholder = new Label(parent, SWT.NONE);
        placeholder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        // 创建按钮区域：Span 两列，右下对齐
        Composite buttonComposite = new Composite(parent, SWT.NONE);
        buttonComposite.setLayout(new GridLayout(2, false));
        buttonComposite.setLayoutData(new GridData(SWT.END, SWT.BOTTOM, false, false));

        Button btnSave = new Button(buttonComposite, SWT.PUSH);
        btnSave.setText("Save");

        Button btnCancel = new Button(buttonComposite, SWT.PUSH);
        btnCancel.setText("Cancel");
    }

    /**
     * 辅助方法：创建带 Label 和 Text 的组合
     */
    private static Text createLabeledText(Composite parent, String labelText) {
        Label label = new Label(parent, SWT.NONE);
        label.setText(labelText);

        Text text = new Text(parent, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        return text;
    }

}
